<template>
  <div class="bg" v-if="visible">
    <div class="container" :class="{ active: !isLogin }">
      <div class="login-account-close" @click="showLoginModal"></div>
      <div class="form-box login">
        <form @submit.prevent="handleLogin">
          <h1>登录</h1>
          <div class="input-box">
            <input type="text" v-model="username" placeholder="账号" required />
            <i class='bx bxs-user'></i>
          </div>
          <div class="input-box">
            <input type="password" v-model="password" placeholder="密码" required />
            <i class='bx bxs-lock-alt'></i>
          </div>
          <button type="submit" class="btn">登录</button>
        </form>
      </div>

      <div class="form-box register">
        <form @submit.prevent="handleRegister">
          <h1>注册</h1>
          <div class="input-box">
            <input type="text" v-model="registerUsername" placeholder="账号" required />
            <i class='bx bxs-user'></i>
          </div>
          <div class="input-box">
            <input type="email" v-model="registerEmail" placeholder="邮箱" required />
            <i class='bx bxs-envelope'></i>
          </div>
          <div class="input-box">
            <input type="password" v-model="registerPassword" placeholder="密码" required />
            <i class='bx bxs-lock-alt'></i>
          </div>
          <button type="submit" class="btn">注册</button>
        </form>
      </div>

      <div class="toggle-box">
        <div class="toggle-panel toggle-left">
          <h1>Hello, Welcome!</h1>
          <p>没有账号?</p>
          <button class="btn register-btn" @click="toggleForm">注册</button>
        </div>

        <div class="toggle-panel toggle-right">
          <h1>Welcome Back!</h1>
          <p>已经有账号?</p>
          <button class="btn login-btn" @click="toggleForm">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import api from '@/api/axios.js'
import { ElMessage } from 'element-plus'
import { useStore } from 'vuex'

const isLogin = ref(true)
const username = ref('')
const password = ref('')
const registerUsername = ref('')
const registerEmail = ref('')
const registerPassword = ref('')
const store = useStore()

// 切换表单
const toggleForm = () => {
  isLogin.value = !isLogin.value
}

// 登录处理
const handleLogin = async () => {
  // 处理登录逻辑
  const res = await api.toLogin({
    account: username.value,
    password: password.value
  })
  if (res.data.code === '200' && res.data.msg === 'success') {
    store.commit('user/SET_TOKEN', res.data.token)
    store.state.user.author_name = res.data.data.author_name
    store.state.user.avatar_url = res.data.data.avatar_url
    store.state.user.user_id = res.data.data.user_id
    ElMessage.success('登录成功')
    setTimeout(() => {
      window.location.reload()
    },300)
  }
}

// 注册处理
const handleRegister = async () => {
  // 处理注册逻辑
  console.log('Registering with:', registerUsername.value, registerEmail.value, registerPassword.value)
  const res = await api.toRegister({
    account: registerUsername.value,
    email: registerEmail.value,
    password: registerPassword.value
  })
  ElMessage.success('注册成功')
  toggleForm()
  console.log(res)
}

const visible = ref(true)

const showLoginModal = () => {
  visible.value = false
  setTimeout(() => {
    store.commit('user/CHANGE_DIALOG_STATE')
  }, 300)
}
</script>

<style scoped>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@100;400;600&display=swap');

.login-account-close {
  width: 36px;
  height: 36px;
  cursor: pointer;
  background: url(../assets/icon/close.png) 50% / contain no-repeat;
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 10;
}

.bg{
  width: 100%;
  height: 100%;
  position: absolute !important;
  z-index: 10 !important;
  top: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.5);
}

.container {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 850px;
  height: 550px;
  background: #fff;
  margin: 20px;
  border-radius: 30px;
  box-shadow: 0 0 30px rgba(0, 0, 0, .2);
  overflow: hidden;
}

.container h1 {
  font-size: 36px;
  margin: -10px 0;
}

.container p {
  font-size: 14.5px;
  margin: 15px 0;
}

form {
  width: 100%;
}

.form-box {
  position: absolute;
  right: 0;
  width: 50%;
  height: 100%;
  background: #fff;
  display: flex;
  align-items: center;
  color: #333;
  text-align: center;
  padding: 40px;
  z-index: 1;
  transition: .6s ease-in-out 1.2s, visibility 0s 1s;
}

.container.active .form-box {
  right: 50%;
}

.form-box.register {
  visibility: hidden;
}

.container.active .form-box.register {
  visibility: visible;
}

.input-box {
  position: relative;
  margin: 30px 0;
}

.input-box input {
  width: 100%;
  padding: 13px 50px 13px 20px;
  background: #eee;
  border-radius: 8px;
  border: none;
  outline: none;
  font-size: 16px;
  color: #333;
  font-weight: 500;
}

.input-box input::placeholder {
  color: #888;
  font-weight: 400;
}

.input-box i {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 20px;
}

.forgot-link {
  margin: -15px 0 15px;
}

.forgot-link a {
  font-size: 14.5px;
  color: #333;
}

.btn {
  width: 100%;
  height: 48px;
  background: #7494ec;
  border-radius: 8px;
  box-shadow: 0 0 10px rgba(0, 0, 0, .1);
  border: none;
  cursor: pointer;
  font-size: 16px;
  color: #fff;
  font-weight: 600;
}

.social-icons {
  display: flex;
  justify-content: center;
}

.social-icons a {
  display: inline-flex;
  padding: 10px;
  border: 2px solid #ccc;
  border-radius: 8px;
  font-size: 24px;
  color: #333;
  margin: 0 8px;
}

.toggle-box {
  position: absolute;
  width: 100%;
  height: 100%;
}

.toggle-box::before {
  content: '';
  position: absolute;
  left: -250%;
  width: 300%;
  height: 100%;
  background: #7494ec;
  border-radius: 150px;
  z-index: 2;
  transition: 1.8s ease-in-out;
}

.container.active .toggle-box::before {
  left: 50%;
}

.toggle-panel {
  position: absolute;
  width: 50%;
  height: 100%;
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  z-index: 2;
  transition: .6s ease-in-out;
}

.toggle-panel.toggle-left {
  left: 0;
  transition-delay: 1.2s;
}

.container.active .toggle-panel.toggle-left {
  left: -50%;
  transition-delay: .6s;
}

.toggle-panel.toggle-right {
  right: -50%;
  transition-delay: .6s;
}

.container.active .toggle-panel.toggle-right {
  right: 0;
  transition-delay: 1.2s;
}

.toggle-panel p {
  margin-bottom: 20px;
}

.toggle-panel .btn {
  width: 160px;
  height: 46px;
  background: transparent;
  border: 2px solid #fff;
  box-shadow: none;
}

@media screen and (max-width: 650px) {
  .container {
    height: calc(100vh - 40px);
  }

  .form-box {
    bottom: 0;
    width: 100%;
    height: 70%;
  }

  .container.active .form-box {
    right: 0;
    bottom: 30%;
  }

  .toggle-box::before {
    left: 0;
    top: -270%;
    width: 100%;
    height: 300%;
    border-radius: 20vw;
  }

  .container.active .toggle-box::before {
    left: 0;
    top: 70%;
  }

  .container.active .toggle-panel.toggle-left {
    left: 0;
    top: -30%;
  }

  .toggle-panel {
    width: 100%;
    height: 30%;
  }

  .toggle-panel.toggle-left {
    top: 0;
  }

  .toggle-panel.toggle-right {
    right: 0;
    bottom: -30%;
  }

  .container.active .toggle-panel.toggle-right {
    bottom: 0;
  }
}

@media screen and (max-width: 400px) {
  .form-box {
    padding: 20px;
  }

  .toggle-panel h1 {
    font-size: 30px;
  }
}
</style>